﻿#include "OiPCH.hpp"
#include "Settings/OiExportDlg.hpp"

namespace Oi
{
    namespace Appl
    {
        class ExportDlgImpl : public Impl<ExportDlg>
        {
        public:
            void init();
            void setupParams();

        public:
            Camera::Device* _camera = nullptr;
            QGridLayout* _toolLayout;
        public:
            int _toolRow = 0;
            int _toolCol = 0;
        };

        void ExportDlgImpl::init()
        {
            OI_Q(ExportDlg)->setupUi(q);

            _toolLayout = new QGridLayout(q->_toolsSection);

            if(!SliceBalance::instance()->simulation)
            {
                auto driver = Camera::DriverManager::instance()->driver(SliceBalance::instance()->cameraDriver.toStdString().c_str());

                if(driver)
                {
                    auto devices = driver->devices();

                    if(devices.size() > 0)
                    {
                        _camera = devices[0];
                    }
                }
            }

            setupParams();
        }

        void ExportDlgImpl::setupParams()
        {
            OI_Q(ExportDlg);

            q->_lines->setValue(SliceBalance::instance()->scanLine);
            q->_flipX->setChecked(SliceBalance::instance()->flipX);
            q->_encoder->setChecked(SliceBalance::instance()->encoderEnable);
            q->_fixedFpsEnable->setChecked(SliceBalance::instance()->fixedFpsEnable);
            q->_laserBrightness->setValue(SliceBalance::instance()->laserBrightness);
            q->_highSpeed->setChecked(SliceBalance::instance()->highSpeed);

            if (_camera)
            {
                auto size = _camera->size();
                _camera->setProperty(Camera::Device::P_ExposureTime, 300);
                _camera->setProperty(Camera::Device::P_WorkMode, Camera::Device::WM_3DONLY);
                _camera->setProperty(Camera::Device::P_Speed, q->_highSpeed->isChecked() ? 1 : 0);
                _camera->setProperty(Camera::Device::P_Peak0XOffset, 0);
                _camera->setProperty(Camera::Device::P_Peak0YOffset, 0);
                _camera->setProperty(Camera::Device::P_Peak0WOffset, size.w());
                _camera->setProperty(Camera::Device::P_Peak0HOffset, size.h());

                q->_fixedFps->setMaximum(_camera->property( Camera::Device::P_MaxFps));
            }

            q->_fixedFps->setValue(SliceBalance::instance()->fixedFps);

            q->_fixedFps->setEnabled(q->_fixedFpsEnable->isChecked());
            q->_freeRunSection->setEnabled(!q->_encoder->isChecked());
        }

        ExportDlg::ExportDlg(QWidget* parent /*= nullptr*/)
            :Ctrl::Dialog(parent)
        {
            OI_I(ExportDlg)->init();
        }

        void ExportDlg::addToolCheck(QString toolName)
        {
            OI_F(ExportDlg);

            d->_toolLayout->addWidget(new QCheckBox(toolName), d->_toolRow, d->_toolCol);

            if (d->_toolCol == 1)
            {
                d->_toolRow++;
                d->_toolCol = 0;
            }
        }

        bool ExportDlg::isToolChecked(QString toolName)
        {
            OI_F(ExportDlg);

            for (int r = 0; r < d->_toolLayout->rowCount(); r++)
            {
                for(int c = 0; c < d->_toolLayout->columnCount(); c++)
                {
                    auto item = d->_toolLayout->itemAtPosition(r, c);
                    if (item && !item->isEmpty())
                    {
                        auto toolCheck = qobject_cast<QCheckBox*>( item->widget() );
                        if (toolCheck && toolCheck->text() == toolName)
                        {
                            return toolCheck->isChecked();
                        }
                    }
                }
            }

            return false;
        }

        void ExportDlg::on__encoder_stateChanged(int)
        {
            _freeRunSection->setEnabled(!_encoder->isChecked());
        }

        void ExportDlg::on__fixedFpsEnable_stateChanged(int)
        {
            _fixedFps->setEnabled(_fixedFpsEnable->isChecked());
        }

        void ExportDlg::on__highSpeed_stateChanged(int)
        {
            OI_F(ExportDlg);
            if(d->_camera)
            {
                d->_camera->setProperty(Camera::Device::P_Speed, _highSpeed->isChecked() ? 1 : 0);

                _fixedFps->setMaximum(d->_camera->property(Camera::Device::P_MaxFps));
            }
        }

    }
}